import { selSmsRecord, reSend, reSendById, reSetSendTime, delTimerById } from '@/services/smsRecord';
import { errNotification, successNotification } from '@/utils/notification';

export default {
    namespace: 'smsRecordAdmin',
  
    state: {
      smsRecordList: [],
      smsRecordListPage: 1,
      smsRecordListTotal: 0,
      longTotal:0,
      shortTotal: 0,
      smsTotal: 0
    },
  
    effects: {
      *fetchSelSmsRecord(param, { call, put }) {
        const response = yield call(selSmsRecord, param);
        console.log(response)
        if(response.success){
          yield put({
            type: 'selSmsRecord',
            payload: response.data,
          });
        }
      },
      *fetchReSend(param, { call, put }) {
        const response = yield call(reSend, param);
        console.log(response)
        if(response.success){
          successNotification("提交成功","")
          const response2 = yield call(selSmsRecord, param);
          if(response2.success){
            yield put({
              type: 'selSmsRecord',
              payload: response2.data,
            });
          }
        }else{
          errNotification("提交失败","")
        }
      },
      *fetchReSendById(param, { call, put }) {
        const response = yield call(reSendById, param);
        console.log(response)
        if(response.success){
          successNotification("提交成功","")
          const response2 = yield call(selSmsRecord, param);
          if(response2.success){
            yield put({
              type: 'selSmsRecord',
              payload: response2.data,
            });
          }
        }else{
          errNotification("提交失败","")
        }
      },
      *fetchReSetSendTime(param, { call, put }) {
        const response = yield call(reSetSendTime, param);
        if(response.success){
          successNotification("设置成功","")
          const response2 = yield call(selSmsRecord, param);
          if(response2.success){
            yield put({
              type: 'selSmsRecord',
              payload: response2.data,
            });
          }
        }else{
          errNotification("设置失败",response.subMsg)
        }
      },
      *fetchDelTimerById(param, { call, put }) {
        const response = yield call(delTimerById, param);
        if(response.success){
          if(response.data.count !== 0){
            successNotification("删除成功","")
          }else{
            errNotification("删除失败","短信已发送或不存在")
          }
          const response2 = yield call(selSmsRecord, param);
          if(response2.success){
            yield put({
              type: 'selSmsRecord',
              payload: response2.data,
            });
          }
        }else{
          errNotification("删除失败",response.subMsg)
        }
      },
    },
  
    reducers: {
      selSmsRecord(state, action) {
          let list = action.payload.content.list
          if(list){
              list.map((item,index)=>{
               item.key = item.id
               if(item.children){
                item.children.map((item,index)=>{
                  item.key = item.id
                })
               }
              })
          }
          return {
          ...state,
          smsRecordList: list,
          smsRecordListPage: action.payload.content.pageNum,
          smsRecordListTotal: action.payload.content.total,
          longTotal: action.payload.longTotal,
          shortTotal: action.payload.shortTotal,
          smsTotal: action.payload.smsTotal
          };
      },
  },
};